package cn.edu.sdjzu.xg2106.service;

import cn.edu.sdjzu.xg2106.dao.TeacherDao;
import cn.edu.sdjzu.xg2106.domain.Department;
import cn.edu.sdjzu.xg2106.domain.Teacher;
import cn.edu.sdjzu.xg2106.domain.University;
import cn.edu.sdjzu.xg2106.exception.BysjException;
import cn.edu.sdjzu.xg2106.util.Condition;
import cn.edu.sdjzu.xg2106.util.JdbcHelper;
import cn.edu.sdjzu.xg2106.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class TeacherService {
    private TeacherService(){}
    public static TeacherService teacherService=null;
    public static TeacherDao teacherDao = TeacherDao.getInstance();
    public static TeacherService getInstance(){
        if (teacherService==null){
            teacherService=new TeacherService();
        }
        return teacherService;
    }
    public Collection<Teacher> findAll(List<Condition> conditionList, Pagination pagination) throws SQLException {
        Connection connection= JdbcHelper.getConn();
        Collection<Teacher> teachers = teacherDao.findAll(connection,conditionList,pagination);
        JdbcHelper.close(connection);
        return teachers;
    }

    public Teacher find(int id) throws SQLException {
        Teacher teacher = teacherDao.find(id);
        return teacher;
    }
    public void delete(int id) throws SQLException {
        teacherDao.delete(id);
    }
    public void add(Teacher teacher) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        teacherDao.add(teacher,connection);
        JdbcHelper.close(null,connection);
    }
    public void upDate(Teacher teacher)throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        teacherDao.update(teacher,connection);
        JdbcHelper.close(null,connection);

    }
    public int countAll(Department departmentToDelete) throws SQLException, ClassNotFoundException {
        Connection connection = JdbcHelper.getConn();
        int countTeachersOfDepartment = teacherDao.countAll(departmentToDelete);
        JdbcHelper.close(connection);
        return countTeachersOfDepartment;
    }
}
